const app = getApp()
var info;
var userimageurl= ''
var userid=''
Page({
  data:{
    userInfo: {},
    dispalt_button: true,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    show_the_picture: 'http://img.mp.itc.cn/upload/20170720/0fa1a67355e54fe09808cb679276646a_th.jpg',
    openid:'',
    token:'',
  },
  bindGetUserInfo(res, e, options) {
      wx.showLoading({
        title: '授权中...',
        icon: 'loading',
        duration: 1000
      })
        var that = this
        info = res;
        //console.log(info)
        that.setData({
          userInfo: info
        })
        userimageurl = res.detail.userInfo.avatarUrl
        //用户信息
        //console.log(that.data.userInfo)
        if (info.detail.userInfo) {
          console.log(info.detail.userInfo)
          wx.login({
            success: function (res) {
              if (res.code) {
                console.log(res.code),
                wx.request({
                  url: 'http://10.20.58.235:8080/v1/open/user/manager/',
                  data: {
                    code: res.code,
                    nickname: info.detail.userInfo.nickName,
                    city: info.detail.userInfo.city,
                    province: info.detail.userInfo.province,
                    headPortrait: userimageurl,
                    country: info.detail.userInfo.country,
                  },
                  method:'POST',
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    //获取后台返回的数据openid,userid,tock
                    console.log('========', res)
                    userid=res.data.userid;
                    wx.setStorage({
                      key: 'userid',
                      data: userid
                    })
                    that.setData({
                      //openid: res.data.openid,
                      userid:res.data.userid
                    })
                    app.globalData.openid = res.data.openid;
                    //判断用户是否注册
                    if (res.data.token==null){//新用户
                      that.setData({
                        dispalt_button:null ,
                      });
                    }else{
                        that.setData({
                          dispalt_button: false,
                        })
                    }
                  }
                })
              } else {
                wx.showModal({
                  content: '授权失败，请重新授权。',
                  showCancel:false,
                  success:function(res){
                    if (res.confirm){
                      wx.redirectTo({
                        url: 'index',
                      })
                    }
                  }
                })
              }
            },
            
          })
        } else {
          wx.showModal({
            content: '您点击了拒绝，如果要使用，请重新授权。',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: 'index',
                })
              }
            }
          })
        }
      },
   onLoad: function (e, openid){
    var that = this
   wx.getUserInfo({
       success: function (re) {
         that.setData({
            nickName: re.userInfo.nickName,
            avatarUrl: re.userInfo.avatarUrl,
         })   
        }
     })
   },
})